package com.atguigu.myclock;

import java.util.concurrent.locks.ReentrantLock;

class  Ticket{
    private  int number=30;
//    public  synchronized  void  sale(){
//        if (number>0){
//            System.out.println(Thread.currentThread().getName()+"卖出第:"+(number--)+"还剩下:"+number);
//        }

//public    void  sale() {
//    synchronized (Object.class) {
//        if (number > 0) {
//            System.out.println(Thread.currentThread().getName() + "卖出第:" + (number--) + "还剩下:" + number);
//        }
//    }
    private  final ReentrantLock lock=new ReentrantLock();
    public  void  sale(){
        lock.lock();
        try{
            if (number > 0) {
        System.out.println(Thread.currentThread().getName() + "卖出第:" + (number--) + "还剩下:" + number);
      }
        }finally{
              lock.unlock();
         }

}
}
public class SaleTickets {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
    /*    new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 51; i++) {
                    ticket.sale();
                }
            }
        },"A").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 51; i++) {
                    ticket.sale();
                }
            }
        },"B").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 51; i++) {
                    ticket.sale();
                }
            }
        },"C").start();*/

        //lambda表达式
        new Thread(()-> {for (int i = 0; i < 51; i++) ticket.sale();},"A").start();
        new Thread(()-> {for (int i = 0; i < 51; i++) ticket.sale();},"B").start();
        new Thread(()-> {for (int i = 0; i < 51; i++) ticket.sale();},"C").start();
    }

}
